<!-- 合同-合同设置-合同异动及原因设置 -->
<template>
  <ContentCard>
    <template #header>
      <pageHeaderNew :title="title" showBack></pageHeaderNew>
    </template>
    <div class="wrap">
      <div class="left-list">
        <div @click="collapse" class="collapseBtn">
          <IconCaretRight v-if="isCollapsed" />
          <IconCaretLeft v-else />
        </div>
        <a-layout class="layout-demo">
          <a-layout-sider
            collapsible
            breakpoint="xl"
            hide-trigger
            :collapsed="isCollapsed"
            :collapsed-width="0"
          >
            <div v-show="!isCollapsed">
              <div class="header">
                <span class="title">合同异动类型</span>
                <u></u>
              </div>
              <a-scrollbar style="height: 600px; overflow: auto">
                <div class="scroll">
                  <div
                    class="scroll-item"
                    v-for="item in CONTRACT_TYPE_CHANGE"
                    :key="item.id"
                    @click="itemClick(item.id)"
                    :class="currId === item.id ? 'item-active' : ''"
                  >
                    {{ item.label }}
                  </div>
                </div>
              </a-scrollbar>
            </div>
          </a-layout-sider>
        </a-layout>
      </div>
      <div class="main-list">
        <ConditionBox :margin-bottom="20">
          <template #right>
            <a-button type="primary" @click="add">新增异动原因</a-button>
            <a-button type="outline" @click="del(null)" status="danger"
              >批量删除</a-button
            >
          </template>
        </ConditionBox>
        <table-turn
          :table-data="getTableData"
          ref="tableRef"
          :is-show-pagination="false"
          :args="args"
        >
          <a-table-column :width="80">
            <template #title> 序号 </template>
            <template #cell="{ rowIndex }">
              {{ rowIndex + 1 }}
            </template>
          </a-table-column>
          <a-table-column :width="300" data-index="text" title="原因描述">
          </a-table-column>
          <a-table-column :width="100" data-index="enable" title="启用状态">
            <template #cell="{ record: { enable } }">
              <a-tag
                class="enable"
                :color="
                  enable === 1 ? `rgb(var(--green-1))` : 'rgb(var(--danger-1))'
                "
                :style="{
                  color:
                    enable === 1
                      ? `rgb(var(--green-6))`
                      : 'rgb(var(--danger-6))',
                  '--enable':
                    enable === 1
                      ? `rgb(var(--green-6))`
                      : 'rgb(var(--danger-6))'
                }"
              >
                {{ enable === 1 ? '启用' : '停用' }}
              </a-tag>
            </template>
          </a-table-column>
          <a-table-column :width="200" data-index="" title="操作" fixed="right">
            <template #cell="{ record }">
              <a-tooltip content="编辑">
                <IconFont
                  type="icon-edit"
                  :size="26"
                  :style="{
                    marginRight: '10px',
                    cursor: 'pointer'
                  }"
                  @click="goEdit(record)"
                />
              </a-tooltip>
              <a-tooltip content="删除" position="left">
                <a-popconfirm
                  content="是否确认删除该条数据?"
                  @ok="del(record.id)"
                >
                  <icon-font
                    type="icon-delete"
                    :size="26"
                    :style="{
                      cursor: 'pointer',
                      marginRight: '10px'
                    }"
                  />
                </a-popconfirm>
              </a-tooltip>
            </template>
          </a-table-column>
        </table-turn>
      </div>
    </div>
  </ContentCard>
  <AddChange
    v-model:visible="modalVisible"
    :list="CONTRACT_TYPE_CHANGE"
    :data="currData"
    :currTypeId="currId"
    @changeAddEmit="changeAddEmit"
  ></AddChange>
</template>

<script lang="ts" setup>
import {
  AddContractStatusType,
  GetContractStatusType,
  PutContractStatusType,
  DelContractStatusType
} from '@/apis/management/contract/baseSetting';
import { Message, Modal } from '@arco-design/web-vue';
import AddChange from './Components/AddChange/index.vue';
import { CONTRACT_TYPE_CHANGE } from '@/utils/constants';

const title = '合同异动及原因设置';

let currId = ref(1); // 储存左侧列表选中的id

// 左侧item点击事件
const itemClick = (id: number) => {
  if (currId.value !== id) {
    currId.value = id;
    tableRef.value.loadData();
  }
};

let modalVisible = ref(false); // 判断新增弹出框是否打开

// 新增异动事件
const add = () => {
  currData.value = {};
  modalVisible.value = true;
};

const tableRef = ref(); // 表格的ref

// 选择器配置
const args = reactive({
  rowSelection: {
    type: 'checkbox',
    showCheckedAll: true,
    onlyCurrent: false
  }
});
/* 储存选择的数据 */
const selectedList = computed(() => tableRef.value.selectedList);

let originData: any = ref([]); // 储存异动类型
let currData = ref(); // 储存当前选中数据
// 获取表格数据
const getTableData = async () => {
  try {
    const resp = await GetContractStatusType({
      type: currId.value //异动类型
    });
    originData.value = resp || [];
    // const res: any = originData.value.find(
    //   (item: any) => item.id === currId.value
    // );
    // res.content = res.personnelChange;
    return Promise.resolve(originData.value);
  } catch (error: unknown) {
    console.log(error);
  }
};

let isCollapsed = ref(false); // 判断是否收起
const collapse = (collapsed: boolean) => {
  isCollapsed.value = !isCollapsed.value;
};

const goEdit = (record: any) => {
  console.log(record);
  currData.value = record;
  modalVisible.value = true;
};

// 删除某一行
const del = async (id: number) => {
  try {
    if (!id) {
      if (selectedList.value.length === 0) {
        Message.error('请至少选择一项！');
      } else {
        Modal.confirm({
          title: '提示',
          content: '确定要删除选中的数据吗？',
          onOk: async () => {
            try {
              await DelContractStatusType({
                ids: selectedList.value
              });
              tableRef.value.selectedList = [];
              tableRef.value.loadData();
            } catch (error: unknown) {
              console.log(error);
            }
          }
        });
      }
    } else {
      await DelContractStatusType({
        ids: [id]
      });
      tableRef.value.selectedList = [];
      tableRef.value.loadData();
    }
  } catch (error: unknown) {
    console.log(error);
  }
};

// 修改状态
const stateChange = async (record: any) => {
  try {
    await PutContractStatusType(record);
    tableRef.value.loadData();
  } catch (error: unknown) {
    console.log(error);
  }
};

// 新增弹出框返回的
const changeAddEmit = async (formData: any) => {
  try {
    if (!formData) {
      currData.value = {};
    }
    // 编辑
    else if (formData.id) {
      await PutContractStatusType(formData);
    }
    // 新增
    else {
      await AddContractStatusType(formData);
    }
    tableRef.value.loadData();
  } catch (error: unknown) {
    console.log(error);
  }
};
</script>

<style scoped lang="less">
.wrap {
  display: flex;
  .left-list {
    position: relative;
    padding: 15px;

    .collapseBtn {
      position: absolute;
      z-index: 9;
      top: 40%;
      right: 15px;
      height: 70px;
      width: 15px;
      background-color: #eee;
      line-height: 70px;
      text-align: center;
      cursor: pointer;
    }

    .layout-demo {
      height: 100%;
    }
    .header {
      display: flex;
      justify-content: space-between;
      height: 60px;
      border-bottom: 1px solid #ccc;
      .title {
        line-height: 60px;
        // position: relative;
        // right: 55px;
      }
      .title::before {
        position: relative;
        top: 5px;
        left: 10px;
        display: inline-block;
        height: 20px;
        width: 5px;
        margin-right: 20px;
        border-radius: 2px;
        // transform: translateY(-50%);
        content: '';
        background-color: #165dff;
      }
    }

    // height: 300px;
    .scroll {
      padding: 10px;

      &-item {
        height: 50px;
        line-height: 50px;
        padding-left: 10px;
        cursor: pointer;
      }
      &-item:hover {
        background-color: #f2f3f5;
        color: #165dff;
      }
    }
    .item-active {
      background-color: #f2f3f5;
      color: #165dff;
    }

    .item-active::before {
      position: absolute;
      display: inline-block;
      height: 50px;
      right: 0px;
      width: 5px;
      margin-right: 10px;
      border-radius: 2px;
      // transform: translateY(-50%);
      content: '';
      background-color: #165dff;
    }
  }

  .main-list {
    flex: 4;
    margin-left: 20px;
  }
}

.enable::before {
  content: '';
  display: inline-block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  position: relative;
  right: 3px;
  background-color: var(--enable);
}
</style>
